<?php

namespace app\api\controller;

use app\api\model\question\Questionnaire;
use app\api\model\question\Topic;
use app\api\model\Setting as SettingModel;
use app\api\model\student\AnswerSheet;
use app\api\model\student\Sign as Model;

class Sign extends Controller
{
    public function list($study = 0)
    {
        $model = new Model;
        $list = $model->getList($this->user['user']->student_user_id, $study);
        return $this->renderSuccess($list);
    }

    public function saveSignImg()
    {
        $model = new Model;
        // 更新记录
        if ($model->saveSignImg($this->postForm())) {
            return $this->renderSuccess('提交成功');
        }
        return $this->renderError($model->getError() ?: '提交失败');
    }

    public function saveFaceImg()
    {
        $model = new Model;
        // 更新记录
        if ($model->saveFaceImg($this->postForm(), $this->user['user'])) {
            return $this->renderSuccess('提交成功');
        }
        return $this->renderError($model->getError() ?: '提交失败');
    }


    public function verifyStudyTime($student_sign_id)
    {
        $sign = (new Model)
            ->field('student_sign_id,job_id,job_level,questionnaire_id,learning_duration')
            ->where([
                'student_sign_id' => $student_sign_id,
                'is_delete' => 0
            ])->find();
        if (!$sign) {
            return $this->renderError('学员信息不存在！');
        }

        $values = SettingModel::getItem('examination');
        $compulsory_learning = !empty($values['compulsory_learning']) ? $values['compulsory_learning'] : 0;
        if ($sign->learning_duration < $compulsory_learning) {
            return $this->renderError('学习时长不足以考试，请前往学习！');
        }

        return $this->renderSuccess('满足考试');
    }

    public function getAnswer($student_sign_id)
    {
        $sign = (new Model)
            ->field('student_sign_id,job_id,job_level,questionnaire_id,learning_duration')
            ->where([
                'student_sign_id' => $student_sign_id,
                'is_delete' => 0
            ])->find();
        if (!$sign) {
            return $this->renderError('学员信息不存在！');
        }

        $values = SettingModel::getItem('examination');
        $compulsory_learning = !empty($values['compulsory_learning']) ? $values['compulsory_learning'] : 0;
        if ($sign->learning_duration < $compulsory_learning) {
            return $this->renderError('学习时长不足以考试，请前往学习！');
        }


        $questionnaire_info = (new Questionnaire())
            ->field('questionnaire_id,answering_time,passing_mark')
            ->where([
                'questionnaire_id' => $sign->questionnaire_id,
                'is_delete' => 0,
                'status' => 0
            ])
            ->find();
        if (!$questionnaire_info) {
            return $this->renderError('问卷不存在！');
        }

        $questionnaire_info->answering_time = $questionnaire_info->answering_time * 60;

        $questionnaire_topic_list = (new Topic())->getList($sign->questionnaire_id);

        $is_face = !empty($values['is_face']) ? 1 : 0;

        return $this->renderSuccess(compact('sign', 'questionnaire_topic_list', 'questionnaire_info', 'is_face'));
    }

    public function handInAPaper()
    {
        $model = new Model;
        // 更新记录
        if ($answer_sheet_id = $model->handInAPaper($this->postForm())) {
            return $this->renderSuccess(['answer_sheet_id' => $answer_sheet_id], '提交成功');
        }
        return $this->renderError($model->getError() ?: '提交失败');
    }

    public function subStudyTime($student_sign_id = 0)
    {
        $model = Model::detail($student_sign_id);
        if ($model) {
            $model->subStudyTime();
        }
        return $this->renderSuccess('提交成功');
    }

    public function getAnswerSheetInfo($answer_sheet_id = 0)
    {
        $answer_sheet = AnswerSheet::detail($answer_sheet_id);
        return $this->renderSuccess(compact('answer_sheet'));
    }
}
